<?php
namespace Models;
use Phalcon\Db\RawValue;

class Setting extends BaseModel {


    public function beforeValidationOnCreate()
    {
        if (!isset($this->svalue) || !$this->svalue) {
            $this->svalue = new RawValue('default');
        }
    }

    public function beforeValidationOnUpdate()
    {
        if (!isset($this->svalue) || !$this->svalue) {
            $this->svalue = new RawValue('default');
        }
    }

    public function notSave()
    {
        //Obtain the flash service from the DI container
        $flash = $this->getDI()->getFlash();

        //Show validation messages
        foreach ($this->getMessages() as $message) {
            $flash->error($message);
        }
    }

    public function beforeUpdate()
    {
        if(is_array($this->svalue)){
            $this->svalue = serialize($this->svalue).'|array';
        }
    }

    public function afterFetch()
    {
        if(substr($this->svalue, -6) == '|array'){
            $this->svalue = unserialize($this->svalue);
        }
    }    

}